﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Runtime.InteropServices;

namespace WHW_NetUtility
{
        /// <summary>
        /// ini文件类
        /// </summary>
        public class IniHelper
        {
            private string m_FileName;

            public string FileName
            {
                get { return m_FileName; }
                set { m_FileName = value; }
            }
        /// <summary>
        /// 读取INI整型值
        /// </summary>
        /// <param name="lpAppName">INI文件中的一个字段名，在这个字段下查找（读取）</param>
        /// <param name="lpKeyName">lpAppName字段下的一个键名，要读取的设置项（条目或者键）</param>
        /// <param name="nDefault">指定条目未找到时返回的默认值</param>
        /// <param name="lpFileName">要查找的文件的名字，如果未指定完整路径，则会在windows路径中搜索文件</param>
        /// <returns>查找的指定条目（键）的返回值，如果键值包含数字和非数字字符，则只返回数字</returns>
            [DllImport("kernel32.dll")]
            private static extern int GetPrivateProfileInt(string lpAppName,string lpKeyName,int nDefault,string lpFileName);
        /// <summary>
        /// 读取INI文件
        /// </summary>
        /// <param name="lpAppName">INI文件中的一个字段名，在这个字段下查找（读取）</param>
        /// <param name="lpKeyName">lpAppName字段下的一个键名，要读取的设置项（条目或者键）</param>
        /// <param name="lpDefault">指定条目未找到时返回的默认值</param>
        /// <param name="lpReturnedString">接收INI文件中的值的CString对象，即接收缓冲区</param>
        /// <param name="nSize">接收缓冲区的大小</param>
        /// <param name="lpFileName">要查找的文件的名字，要指定完整路径名，可以写成"c:\\usefile\\usefile.ini"或".\\student.ini"，后者表示程序目录</param>
        /// <returns>返回值非零，表示读取成功，为零表示读取失败</returns>
        [DllImport("kernel32.dll")]
            private static extern int GetPrivateProfileString(string lpAppName,string lpKeyName,string lpDefault,StringBuilder lpReturnedString,int nSize,string lpFileName);
        /// <summary>
        /// 写入INI文件
        /// </summary>
        /// <param name="lpAppName">INI文件中的一个字段名</param>
        /// <param name="lpKeyName">lpAppName字段下的一个键名，也不是变量名</param>
        /// <param name="lpString">键值，也就是数据</param>
        /// <param name="lpFileName">INI文件完整的路径名</param>
        /// <returns>返回值非零，表示写入成功，为零表示写入失败</returns>
           [DllImport("kernel32.dll")]
            private static extern int WritePrivateProfileString(string lpAppName,string lpKeyName,string lpString,string lpFileName);

            /// <summary>
            /// 构造函数
            /// </summary>
            /// <param name="aFileName">Ini文件路径</param>
            public IniHelper(string aFileName)
            {
                this.m_FileName = aFileName;
            }

            /// <summary>
            /// 构造函数
            /// </summary>
            public IniHelper(){ }

            /// <summary>
            /// [扩展]读Int数值
            /// </summary>
            /// <param name="section">节</param>
            /// <param name="name">键</param>
            /// <param name="def">默认值</param>
            /// <returns></returns>
            public int ReadInt(string section, string name, int def)
            {
                return GetPrivateProfileInt(section, name, def, this.m_FileName);
            }

            /// <summary>
            /// [扩展]读取string字符串
            /// </summary>
            /// <param name="section">节</param>
            /// <param name="name">键</param>
            /// <param name="def">默认值</param>
            /// <returns></returns>
            public string ReadString(string section, string name, string def)
            {
                StringBuilder vRetSb = new StringBuilder(2048);
                GetPrivateProfileString(section, name, def, vRetSb, 2048, this.m_FileName);
                return vRetSb.ToString();
            }

            /// <summary>
            /// [扩展]写入Int数值，如果不存在 节-键，则会自动创建
            /// </summary>
            /// <param name="section">节</param>
            /// <param name="name">键</param>
            /// <param name="Ival">写入值</param>
            public void WriteInt(string section, string name, int Ival)
            {

                WritePrivateProfileString(section, name, Ival.ToString(), this.m_FileName);
            }

            /// <summary>
            /// [扩展]写入String字符串，如果不存在 节-键，则会自动创建
            /// </summary>
            /// <param name="section">节</param>
            /// <param name="name">键</param>
            /// <param name="strVal">写入值</param>
            public void WriteString(string section, string name, string strVal)
            {
                WritePrivateProfileString(section, name, strVal, this.m_FileName);
            }

            /// <summary>
            /// 删除指定的 节
            /// </summary>
            /// <param name="section"></param>
            public void DeleteSection(string section)
            {
                WritePrivateProfileString(section, null, null, this.m_FileName);
            }

            /// <summary>
            /// 删除全部 节
            /// </summary>
            public void DeleteAllSection()
            {
                WritePrivateProfileString(null, null, null, this.m_FileName);
            }

            /// <summary>
            /// 读取指定 节-键 的值
            /// </summary>
            /// <param name="section"></param>
            /// <param name="name"></param>
            /// <returns></returns>
            public string IniReadValue(string section, string name)
            {
                StringBuilder strSb = new StringBuilder(256);
                GetPrivateProfileString(section, name, "", strSb, 256, this.m_FileName);
                return strSb.ToString();
            }

            /// <summary>
            /// 写入指定值，如果不存在 节-键，则会自动创建
            /// </summary>
            /// <param name="section"></param>
            /// <param name="name"></param>
            /// <param name="value"></param>
            public void IniWriteValue(string section, string name, string value)
            {
                WritePrivateProfileString(section, name, value, this.m_FileName);
            }
        }
}
